import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(()->{
            synchronized (locker){
                System.out.println("t1等待之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1等待之后");
            }
        });

        Thread t2 = new Thread(()->{
            Scanner sc = new Scanner(System.in);
            synchronized (locker){
                System.out.println("t2通知之前");
                //当没有 sc.next()时
                //由于 t1 和 t2 执行的顺序不确定,有可能是 t2 先执行了 notify,此时 t1 还没有wait
                locker.notify();
                System.out.println("t2通知之后");
            }
            //此时 t2 才会完全释放锁
        });

        t1.start();
        t2.start();
        //notify 操作必须在 wait 之前,否则将会一直等待,无法改变 wait(但是也不会抛出异常)
    }
}
